#!/usr/bin/env python

import string

class Permuter:
    def __init__(self, sequence):
        self.the_input = sequence
    #end def __init__

#end class Permuter


def reverse(input):
    inlist = list(input)
    inlist.reverse()
    return string.join(inlist, '')

def next_permutation(this_permutation):
    if len(this_permutation) < 2:
        return None

    first = len(this_permutation) - 2
    second = len(this_permutation) - 1
    while first >= 0 and this_permutation[second] <= this_permutation[first]:
        (second, first) = (first, first - 1)

    if first < 0:
        return None

    j = len(this_permutation) - 1
    while this_permutation[first] >= this_permutation[j]:
        j = j - 1

#      print 'this_permutation =', this_permutation
#      print "first =", first
#      print "second =", second
#      print "j =", j
    

    this_permutation = ( this_permutation[0:first] + 
                         this_permutation[j] +
                         this_permutation[first + 1:j] +
                         this_permutation[first] + 
                         this_permutation[j + 1:] )
    result = this_permutation[0:second] + reverse(this_permutation[second:])
    return result
    
    # now do something else

# end def this_permutation

def do_main():
    import sys
    output = sys.argv[1]

    while output:
        print output
        output = next_permutation( output )
        
# end def do_main


if __name__ == "__main__":
    do_main()
